package me.zfz.business.controller.impl;

import me.zfz.base.bean.SmartResponse;
import me.zfz.base.dao.impl.BaseDaoImpl;
import me.zfz.base.service.impl.BaseServiceImpl;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.JobInfo;
import me.zfz.business.bean.entity.TrainInfo;
import me.zfz.business.constant.IActionConstant;
import me.zfz.business.controller.base.BaseController;
import me.zfz.business.service.JobInfoService;
import me.zfz.business.service.TrainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mixsmart.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 显示页面
 *
 * @author author
 */
@Controller
@RequestMapping("/showPage")
public class ShowPageController extends BaseController {

    @Autowired
    private TrainInfoService trainInfoService;
    @Autowired
    private JobInfoService jobInfoService;

    /**
     * 显示页面
     *
     * @param pagePath 页面路径（目录之间用下划线“_”分隔）
     * @param id       ID
     * @param busiName 业务名称（不建议使用）
     * @param op       操作标识（如：add、edit等，也不建议使用）
     * @return 返回页面视图
     * @throws Exception
     */
    @RequestMapping("/{pagePath}")
    public ModelAndView index(@PathVariable String pagePath,
                              String id, String busiName, String op, HttpServletRequest request) throws Exception {
        ModelAndView modelView = new ModelAndView();
//        UserInfo userInfo = getUserInfoFromSession(request);
//        boolean isAdmin = !Objects.equals("01337e1d2666412fa3d028136ff710f7", userInfo.getRoleIds().get(0));
        if (StringUtils.isNotEmpty(pagePath)) {
            String[] params = pagePath.split("_");
            if (params.length > 0) {
                String dir = "";
                String viewPage = StringUtils.filterFilePath(params[params.length - 1]);
                if (params.length < 5 && params.length > 0) {
                    for (int i = 0; i < (params.length - 1); i++) {
                        dir += StringUtils.filterFilePath(params[i]) + "/";
                    }
                    viewPage = dir + viewPage;
                }
                modelView.setViewName(viewPage);
            }
        }
        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("id", id);

        if (Objects.equals(pagePath,"front_index")) {
            SmartResponse<TrainInfo> trainInfoSmartResponse = trainInfoService.findAll();
            modelMap.put("trainList", trainInfoSmartResponse.getDatas());
            SmartResponse<JobInfo> jobInfoSmartResponse = jobInfoService.findAll();
            modelMap.put("jobList", jobInfoSmartResponse.getDatas());
        }
        if (StringUtils.isNotEmpty(op)) {
            int sortOrder = 0;
            BaseDaoImpl<?> dao = getBaseDao(busiName);
            if (IActionConstant.ADD.equals(op)) {
                sortOrder = (null != dao) ? dao.getSortOrder(id) : sortOrder;
                modelMap.put("sortOrder", sortOrder);
            } else if (IActionConstant.EDIT.equals(op) && null != dao) {
                Object objBean = dao.find(id);
                modelMap.put("objBean", objBean);
            }
        }
        return modelView;
    }

}
